<template>
    <el-form :inline="true">
      <el-form-item>
        <selector v-model="params.difficulty" placeholder="请选择题目难度" style="width: 200px;"></selector>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.title" placeholder="请您输入要搜索的题目标题" />
      </el-form-item>
      <el-form-item>
        <el-button plain type="" @Click="onSearch">搜索</el-button>
        <el-button plain type="info"  @click="onReset">重置</el-button>
        <el-button plain type="primary" :icon="Plus" @click="onAddQuestion" >添加题目</el-button>
      </el-form-item>
    </el-form>
    <el-table height="526px" :data="questionList">
      <el-table-column prop="questionId" width="180px" label="题目id" />
      <el-table-column prop="title" label="题目标题" />
      <el-table-column prop="difficulty" label="题目难度" width="90px">
        <template #default="{ row }">
          <div v-if="row.difficulty === 1" style="color:#3EC8FF;">简单</div>
          <div v-if="row.difficulty === 2" style="color:#FE7909;">中等</div>
          <div v-if="row.difficulty === 3" style="color:#FD4C40;">困难</div>
        </template>
      </el-table-column>
      <el-table-column prop="createName" label="创建人" width="140px" />
      <el-table-column prop="createTime" label="创建时间" width="180px" />
      <el-table-column label="操作" width="100px" fixed="right">
        <template #default="{ row }">
        <el-button type="text" @click="onEdit(row.questionId)" >编辑
        </el-button>
        <el-button type="text" class="red" plain @click="del(row.questionId)"  >删除
          <!-- @click="onDelete(row.questionId)  -->
        </el-button>
      </template>
      </el-table-column>
    </el-table>
    
    <!--  分页设置  需要国际化， 参考elemen-plus中的国际化-->
    <el-pagination
        background size="small" :page-sizes="[1,5, 10, 20, 50]" :total="total"
        layout="total, sizes, prev, pager, next, jumper" class="mt-4"
        v-model:current-page="params.pageNum"  v-model:page-size="params.pageSize"    
        @size-change="handleSizeChange" @current-change="handleCurrentChange"
    />
    <!-- 当我们 使用 v-model 进行绑定之后 ，响应式数据就会根据 界面数据的更换而更换  -->
    
    <!-- 抽屉： 开始实现编辑（添加题目） 的功能  -->
    <question-drawer ref="questionEditRef"  @success="onSuccess"></question-drawer>
     
  </template>
  
  <script setup>
  import { ElMessage, ElMessageBox } from 'element-plus'
  import { Plus } from "@element-plus/icons-vue"
  import Selector from "@/components/QuestionSelector.vue"
  import { reactive,ref } from "vue";
  import { deleteQuestionService, getQuestionListService } from "@/apis/question";
  import QuestionDrawer from "@/components/QuestionDrawer.vue";

  
//  这里是 给的默认值( 查询，换页的时候使用 )
  const params = reactive({
    pageNum: 1,
    pageSize: 10,
    difficulty: '',
    title: ''
  })

//  定义响应式数据
  const questionList = ref([])
  const total = ref()
  
//  取值 并展示在 界面上
  async function getQuestionList(){
    const result = await getQuestionListService(params)
    questionList.value= result.rows
    total.value = parseInt(result.total) 
  
  }
  getQuestionList()
  



// 换页时 触发的 事件 
// 绑定之后就不需要我们 进行参数的修改了
  const handleSizeChange = (newSize) => {
    // 这里 我们在选择 newSize 之后选择返回第一页
    // params.pageSize = newSize
    params.pageNum = 1
    getQuestionList()
  }
  const handleCurrentChange = (newPage) => {
    // params.pageNum = newPage
    getQuestionList()
  }




  // 开始搜索功能
  function onSearch(){
    params.pageNum = 1
    getQuestionList()
  }

  function onReset() {
    params.pageNum = 1
    params.pageSize = 10
    params.title = ''
    params.difficulty = ''
    getQuestionList()
  }



  const questionEditRef=ref()

  // 开始添加题目
  function onAddQuestion(){
    questionEditRef.value.open()
  }

  // 添加成功后 刷新页面
  function onSuccess(service) {
    
    if (service === 'add') {
      params.pageNum = 1
    }
    getQuestionList()
  }



  // 编辑题目
  function onEdit(questionId){
    // 打开抽屉
    questionEditRef.value.open(questionId)
  }




  // 删除 题目
  async function del(questionId) {
    await ElMessageBox.confirm(
      '是否确认删除',
      {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning',
      }
    )

    await onDelete(questionId)
    
  }

  async function onDelete(questionId){
    
    deleteQuestionService(questionId)
    ElMessage.success("删除成功")
    params.pageNum = 1
    await getQuestionList()
  }

  </script>